/**
 * Project Name:lincomb-financeCore-manager
 * File Name:OperateFastdfsUtil.java
 * Package Name:com.lincomb.financeCore.manager.utils
 * Date:2016年4月27日下午4:20:13
 * Copyright (c) 2016, 上海联璧电子科技有限公司 All Rights Reserved.
 *
 */

package com.example.demo.utils.file;

import com.example.demo.utils.PropertiesUtil;
import com.lincomb.utils.fastdfs.FastdfsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
*OperateFastdfsUtil
 */
@Slf4j
public class OperateFastdfsUtil
{

    private final static String SYMBOL = ",";

    private static String ips;

    public static String getIps() {
        return ips;
    }

    public static void setIps(String ips) {
        OperateFastdfsUtil.ips = ips;
    }

    /**
     * getFIleHttpAddress:获取文件服务器地址
     * TODO(这里描述这个方法适用条件 – 可选).
     *
     * @author bo.shen
     * @return IP
     * @since JDK 1.7
     */
    public static String getFIleHttpAddress(){
        String IP = "";

        if(!StringUtils.isEmpty(ips)){
            if(ips.contains(SYMBOL)){
                String[] address = ips.split(SYMBOL);
                Random rand = new Random();
                IP = address[rand.nextInt(address.length)];
            }else{
                IP = ips;
            }
        }else{
            IP = "";
        }
        return IP;
    }

    /**
     * 获取fastdfs服务器上图片路径信息, 图片转Base64格式编码串
     * @param base64Image
     * @param filePrefix
     * @param fileSuffix
     * @return
     * @throws Exception
     */
    public static Map<String, Object> getImagePathFromFastdfs(String base64Image, String filePrefix, String fileSuffix) throws Exception{

        Map<String, Object> rtnval = new HashMap<String, Object>();
        try {
            //Base64解码工具类
            BASE64Decoder decoder = new BASE64Decoder();

            // Base64解码
            byte[] buf = decoder.decodeBuffer(base64Image);

            //文件名
            StringBuffer imageName = new StringBuffer("");
            if(StringUtils.isEmpty(imageName)) {
                imageName.append("IMAGE");
            }else{
                imageName.append(filePrefix);
            }

            imageName.append(String.valueOf(Calendar.getInstance().getTime().getTime()));

            if(StringUtils.isEmpty(fileSuffix)){
                imageName.append(".png");
            }else{
                imageName.append(fileSuffix);
            }

            //获取图片输入流
            InputStream imageIS = new ByteArrayInputStream(buf);

            //获取图片大小
            long imageSize = imageIS.available();

            //获得文件所在服务器相对路径
            String img_url = FastdfsUtil.getInstance(PropertiesUtil.getValue("fastdfs.upload.server.ip")).uploadFile(imageIS, imageName.toString(), imageSize);

            //关闭文件流
            imageIS.close();

            rtnval.put("imgPath", img_url);
            rtnval.put("imgName", imageName.toString());

        } catch (Exception e) {
            log.info(e.getMessage());
            throw e;
        }

        return rtnval;
    }

    /**
     * 获取fastdfs服务器上图片路径信息, 图片输入流
     * @param is
     * @param filePrefix
     * @param fileSuffix
     * @return
     * @throws Exception
     */
    public static Map<String, Object> getImagePathFromFastdfs(InputStream is, String filePrefix, String fileSuffix) throws Exception{

        Map<String, Object> rtnval = new HashMap<String, Object>();
        try {
            //文件名
            StringBuffer imageName = new StringBuffer("");
            if(StringUtils.isEmpty(imageName)) {
                imageName.append("IMAGE");
            }else{
                imageName.append(filePrefix);
            }

            imageName.append(String.valueOf(Calendar.getInstance().getTime().getTime()));

            if(StringUtils.isEmpty(fileSuffix)){
                imageName.append(".png");
            }else{
                imageName.append(fileSuffix);
            }

            //获取图片大小
            long imageSize = is.available();

            //获得文件所在服务器相对路径
            String img_url = FastdfsUtil.getInstance(PropertiesUtil.getValue("fastdfs.upload.server.ip")).uploadFile(is, imageName.toString(), imageSize);

            //关闭文件流
            is.close();

            rtnval.put("imgPath", img_url);
            rtnval.put("imgName", imageName.toString());

        } catch (Exception e) {
            log.info(e.getMessage());
            throw e;
        }

        return rtnval;
    }

}

